package com.zhisheng.core.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class CompressUtils {
	private static int cachesize = 1024; 
	
	/**
	 * use ZIP
	 * 
	 * @param str
	 *            
	 * @return 
	 */
	public static final byte[] compressByZip(String str) {
		if (str == null)
			return null;

		byte[] compressed = null;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;

		try {
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(str.getBytes());
			zout.closeEntry();
			compressed = out.toByteArray();
		} catch (IOException e) {
			compressed = null;
		} finally {
			CloseUtils.close(zout, out);
		}

		return compressed;
	}
	
	/**
	 * use ZIP
	 * 
	 * @param compressed
	 * @return 
	 */
	public static final String decompressByZip(byte[] compressed) {
		if (compressed == null)
			return null;

		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zin = null;
		String decompressed;
		try {
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new ZipInputStream(in);
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e) {
			decompressed = null;
		} finally {
			CloseUtils.close(zin, in, out);
		}

		return decompressed;
	}
	
	/**
	 * use ZLIB
	 * @param input
	 * @return
	 */
	public static byte[] compressByZlib(byte input[]) {
		Deflater compresser = new Deflater();
		compresser.setInput(input);
		compresser.finish();
		byte output[] = new byte[0];
		ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
		
		try {
			byte[] buf = new byte[cachesize];
			int got;
			while (!compresser.finished()) {
				got = compresser.deflate(buf);
				o.write(buf, 0, got);
			}
			output = o.toByteArray();
		} finally {
			CloseUtils.close(o);
			compresser.end();
		}
		return output;
	} 
	
	/**
	 * use ZLIB
	 * @param input
	 * @return
	 */
	public static byte[] decompressByZlib(byte input[]) {
		Inflater decompresser = new Inflater(); 
		byte output[] = new byte[0];
		decompresser.setInput(input);
		ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
		try {
			byte[] buf = new byte[cachesize];
			int got;
			while (!decompresser.finished()) {
				got = decompresser.inflate(buf);
				o.write(buf, 0, got);
			}
			output = o.toByteArray();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			CloseUtils.close(o);
			decompresser.end();
		}
		return output;
	}
}
